package com.pay.dynamicsdk.usage;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.pay.dynamicsdk.R;

import java.util.List;

import sdk.pay.model.PayTypeModel;

/**
 * Created by Administrator on 2017/7/7.
 */

public class PayTypeRecycleAdapter extends Adapter<PayTypeRecycleAdapter.MyViewHolder> {
    protected boolean mIsPortrait;
    private List<PayTypeModel> mArrayList;
    private int mSelectedPosition = 0;
    private OnItemClickListener mOnItemClickListener;

    public PayTypeRecycleAdapter(List<PayTypeModel> mArrayList) {
        this.mArrayList = mArrayList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(mIsPortrait ? R.layout.item_listviewlayout : R.layout.item_listviewlayout_land, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (mIsPortrait) {
            holder.image_Checked.setImageResource(mSelectedPosition == position ? R.drawable.image_icon_radiobutton_yes : R.drawable.image_icon_radiobutton_no);
            holder.port_line_view.setVisibility(position == mArrayList.size() - 1 ? View.INVISIBLE : View.VISIBLE);
        } else {
            holder.image_Checked.setVisibility(mSelectedPosition == position ? View.VISIBLE : View.INVISIBLE);
            holder.land_layout.setBackgroundResource(mSelectedPosition == position ? R.drawable.bg_item_paytype_selected : R.drawable.bg_item_paytype_unselected);
        }
        setDescription(holder, position);
        bindItemListener(holder, position);

    }

    private void bindItemListener(final MyViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(holder.itemView, position);
                }
            }
        });
    }

    private void setDescription(MyViewHolder holder, int position) {
        String description = "暂无";
        switch (mArrayList.get(position).getTypeid()) {
            case 3:
                holder.image_junpay_type.setImageResource(R.drawable.u3);
                description = "推荐微信用户使用";
                break;
            case 4:
                holder.image_junpay_type.setImageResource(R.drawable.u4);
                description = "推荐支付宝用户使用";
                break;
            case 11:
                holder.image_junpay_type.setImageResource(R.drawable.u11);
                description = "推荐QQ用户使用";
                break;
            case 12:
                holder.image_junpay_type.setImageResource(R.drawable.u12);
                description = "推荐京东用户使用";
                break;
            default:
                holder.image_junpay_type.setImageURI(null);
                break;
        }
        holder.textView_junpay_type_name.setText(String.format("%s", mArrayList.get(position).getTypename()));
        String contactWay = mArrayList.get(position).getContactWay();
        if (!TextUtils.isEmpty(contactWay)) {
            description = contactWay;
        }
        holder.textView_junpay_type_tips.setText(String.format("%s", description));
    }

    @Override
    public int getItemCount() {
        return mArrayList.size();
    }

    protected static final class MyViewHolder extends RecyclerView.ViewHolder {
        ViewGroup land_layout;
        ImageView image_Checked;
        View port_line_view;
        ImageView image_junpay_type;
        TextView textView_junpay_type_name;
        TextView textView_junpay_type_tips;

        public MyViewHolder(View itemView) {
            super(itemView);
            image_Checked = (ImageView) itemView.findViewById(R.id.ImageButton_junpay_type_Checked);
            port_line_view = itemView.findViewById(R.id.item_listview_layout_view);
            land_layout = (ViewGroup) itemView.findViewById(R.id.item_listview_land_layout);
            image_junpay_type = (ImageView) itemView.findViewById(R.id.image_junpay_type);
            textView_junpay_type_name = (TextView) itemView.findViewById(R.id.TextView_junpay_type_name);
            textView_junpay_type_tips = (TextView) itemView.findViewById(R.id.TextView_junpay_type_tips);
        }
    }

    public void isPortrait(boolean isPortrait) {
        this.mIsPortrait = isPortrait;
    }

    public void selectItem(int position) {
        this.mSelectedPosition = position;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setList(List<PayTypeModel> arrayList) {
        this.mArrayList = arrayList;
    }

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }
}